package com.atguigu.team.service;

import com.atguigu.team.domain.Architect;
import com.atguigu.team.domain.Designer;
import com.atguigu.team.domain.Employee;
import com.atguigu.team.domain.Equipment;
import com.atguigu.team.domain.NoteBook;
import com.atguigu.team.domain.PC;
import com.atguigu.team.domain.Printer;
import com.atguigu.team.domain.Programmer;
 
public class CompanyService {
	
	private Employee[] employees;
	
	public CompanyService() {
		// 先处理所有的设备对象
		// 创建一个设备数组
		Equipment[] eqs = new Equipment[Data.EQUIPMENTS.length];
		for (int i = 2; i < Data.EQUIPMENTS.length; i++) {
			// 获取子数组
			String[] child = Data.EQUIPMENTS[i];
			// 获取识别码, 并转换成整数
			int code = Integer.parseInt(child[0]);
			// 样例数据: {"21", "宏碁 ", "AT7-N52"},
			 // PC      :21, model, display
			if (code == Data.PC) {
				// 当前子数组中的数据就可以整合成一个PC对象
				//public PC(String model, String display) {
				String model = child[1];
				String display = child[2];
				// 对象保存在数组中
				eqs[i] = new PC(model, display);
			} else if (code == Data.NOTEBOOK) {
				//样例 : {"22", "联想Y5", "6000"},
				//NoteBook:22, model, price
				// 当前子数组中的数据就可以整合成一个NoteBook对象
				// public NoteBook(String model, double price) {
				String model = child[1];
				double price = Integer.parseInt(child[2]);
				// 对象保存在数组中
				eqs[i] = new NoteBook(model, price);
			} else if (code == Data.PRINTER) {
				//样例 : {"23", "激光", "佳能 2900"},
				//Printer :23, type, name
				// 当前子数组中的数据就可以整合成一个Printer对象
				// public Printer(String type, String name) {
				String type = child[1];
				String name = child[2];
				// 对象保存在数组中
				eqs[i] = new Printer(type, name);
			}
		}
		/* 测试用
		for (Equipment equipment : eqs) {
			System.out.println(equipment);
		}*/
		
		this.employees = new Employee[Data.EMPLOYEES.length];
		for (int i = 0; i < Data.EMPLOYEES.length; i++) {
			// 获取子数组
			String[] child = Data.EMPLOYEES[i];
			// 获取识别码, 转成整数
			int code = Integer.parseInt(child[0]);
			int id = Integer.parseInt(child[1]);
			String name = child[2];
			int age = Integer.parseInt(child[3]);
			double salary = Integer.parseInt(child[4]);
			//样例{"10", "1", "段誉", "22", "3000"},
			//Employee  :  10, id, name, age, salary
			if (code == Data.EMPLOYEE) {
				// 创建Employee对象
				//public Employee(int id, String name, int age, double salary) {
				//创建好对象后, 保存在数组中
				this.employees[i] = new Employee(id, name, age, salary);
			} else if (code == Data.PROGRAMMER) {
				//{"11", "3", "任我行", "23", "7000"},
				//Programmer:  11, id, name, age, salary
				/*public Programmer(  int id, 
									  String name, 
									  int age, 
									  double salary,
									  int memberId, 
									  Status status, 
									  Equipment equipment) {*/
				//设备对象从上面的数组中获取, 只需要对应的元素即可
				//创建好对象后, 保存在数组中
				this.employees[i] = new Programmer(id,
												   name,
												   age,
												   salary,
												   0,
												   Status.FREE,
												   eqs[i]);
			} else if (code == Data.DESIGNER) {
				//{"12", "5", "周芷若", "28", "10000", "5000"},
				//Designer  :  12, id, name, age, salary, bonus
				/*
				public Designer(int id, 
								String name, 
								int age, 
								double salary, 
								int memberId,
								Status status, 
								Equipment equipment, 
								double bonus) {
				 */
				//设备对象从上面的数组中获取, 只需要对应的元素即可
				double bonus = Integer.parseInt(child[5]);
				//创建好对象后, 保存在数组中
				this.employees[i] = new Designer(id, name, age, salary, 0, Status.FREE, eqs[i], bonus);
			} else if (code == Data.ARCHITECT) {
				//{"13", "2", "令狐冲", "32", "18000", "15000", "2000"},
				//Architect :  13, id, name, age, salary, bonus, stock
				/*
				public Architect(int id, 
								 String name, 
								 int age, 
								 double salary, 
								 int memberId,
								 Status status, 
								 Equipment equipment, 
								 double bonus, 
								 int stock) {
				*/
				//设备对象从上面的数组中获取, 只需要对应的元素即可
				double bonus = Integer.parseInt(child[5]);
				int stock = Integer.parseInt(child[6]);
				//创建好对象后, 保存在数组中
				this.employees[i] = new Architect(id, name, age, salary, 0, Status.FREE, eqs[i], bonus, stock);
			}
		}
		
		//测试用
		/*
		for (int j = 0; j < employees.length; j++) {
			System.out.println(employees[j]);
		}*/
		
	}
	
	/**
	 * 获取所有员工的存储的数组
	 * @return
	 */
	public Employee[] getAllEmployees() {
		/*
		Employee[] copy = new Employee[employees.length];
		for (int i = 0; i < copy.length; i++) {
			copy[i] = employees[i];
		}
		return copy;
		*/
		return employees;
	}
	
	/**
	 * 方法：获取指定ID的员工对象。
	 * @param id 指定员工的ID
	 * @return 指定员工对象
	 * 异常：找不到指定的员工
	 */
	public Employee getEmployee(int id) throws TeamException {
		// 遍历所有员工的数组
		for (int i = 0; i < employees.length; i++) {
			// 如果某员工的ID属性和参数id一样,就返回这个员工对象
			if (employees[i].getId() == id) {
				return employees[i];
			}
		}
		// 如果没有找到对象, 抛出异常 throw 
		throw new TeamException("找不到指定ID[" + id + "]的员工");
	}
	
}
